<template>
  <el-dialog v-model="visible" title="发布通知" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content" style="height: 300px">
        <editor v-model="content" :init="{ height: 300 }"></editor>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import Tinymce from "@/components/tinymce";
export default defineComponent({
  components: { editor: Tinymce },
  setup() {
    return reactive({
      visible: false,
      id: null,
      title: "",
      content: "",
    });
  },
  computed: {
    dataRule() {
      return {
        title: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        content: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }]
      };
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
          if (this.id) {
            this.getInfo();
          }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spmatchitem/" + this.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        let data = res.data;
        if(data){
          this.id = data.id;
          this.title = data.noticeTitle;
          this.content = data.noticeContent;
        }
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      baseService.post("/sp/spmatchitem/updateNotice", {
        id: this.id,
        noticeTitle: this.title,
        noticeContent: this.content
      }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message({
          message: this.$t("prompt.success"),
          type: "success",
          duration: 500,
          onClose: () => {
            this.visible = false;
            this.$emit("refreshDataList");
          }
        });
      });
    }
  }
});
</script>
